package com.yk.faceapplication.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.allinpay.xed.R;
import com.android.volley.VolleyError;
import com.yk.faceapplication.callback.RequestCallback;
import com.yk.faceapplication.entity.StudyInfo;
import com.yk.faceapplication.http.RequestUrl;
import com.yk.faceapplication.util.RequestUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ContactActivity extends BaseActivity {
    List<StudyInfo> contacts;
    TextView phoneTV, wxTV, qqTV;
    String phone = null;

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.getId() == R.id.phone_tv) {
            //拨打电话
            if(phone != null){
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }

        }
    }

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setTitle("联系客服");
        phoneTV = (TextView) findViewById(R.id.phone_tv);
        phoneTV.setOnClickListener(this);
        wxTV = (TextView) findViewById(R.id.wx_tv);
        qqTV = (TextView) findViewById(R.id.qq_tv);
        phoneTV.setTextIsSelectable(true);
        wxTV.setTextIsSelectable(true);
        qqTV.setTextIsSelectable(true);
        if (mApplication.getContacts() != null) {
            contacts = mApplication.getContacts();
            initContactData();
        } else {
            getContact();
        }
    }

    private void initContactData() {

        String time = null;
        for (StudyInfo contact : contacts) {
            if (contact.getCode().equals("xed-contact-us-phone")) {
                phone = contact.getValue();
            } else if (contact.getCode().equals("xed-contact-us-qq")) {
                qqTV.setText(contact.getValue());
            } else if (contact.getCode().equals("xed-contact-us-wx")) {
                wxTV.setText(contact.getValue());
            } else if (contact.getCode().equals("xed-contact-us-online-time")) {
                time = contact.getValue();
            }
        }
        String msg = phone + (time == null ? "" : "<br />("+time+")");
        phoneTV.setText(Html.fromHtml(msg));
    }

    private void getContact() {
        Map<String, String> form = new HashMap<String, String>();

        RequestCallback callback = new RequestCallback() {
            @Override
            public void success(JSONObject jsonObject) {
                try {
                    contacts = JSON.parseArray(jsonObject.getString("data"), StudyInfo.class);
                    mApplication.setContacts(contacts);
                    initContactData();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void fail(VolleyError volleyError) {

            }
        };
        RequestUtil.request(this, RequestUrl.GET_CONTACT + "token=" + mToken, callback, form);
    }
}
